/**
 * Created by niexiaosong on 18/8/1.
 */
var yydmval = '';
var ssmkval = '';
var cszsmval = '';
var loginModeCsz = '';//参数值说明

$(function () {
    getSysList();
});

//初始化复选框样式
function showCss() {
    $('.i-checks').iCheck({
        checkboxClass: 'icheckbox_square-green',
        radioClass: 'iradio_square-green'
    });
}

/**
 * 系统列表
 */
function getSysList() {

    $.ajax({
        type: "GET",
        contentType: 'application/json',
        dataType: "json",
        url: "param/queryApplication",
        success: function (jsonData) {
            if (jsonData.code == 200) {
                setTableValue(jsonData.list);
            }
        }
    });

    //渲染赋值
    function setTableValue(listEntity) {
        var tablehtml = '';
        if (listEntity.length > 0) {
            for (var i = 0; i < listEntity.length; i++) {
                var data = listEntity[i];
                tablehtml += '<tr onclick="getParamList(\'' + data.yydm + '\',this)">';
                tablehtml += '<td align="center" style="padding-left: 0px;padding-right: 0px;">';
                tablehtml += '<span>' + data.yymc + '</span>';
                tablehtml += '</td>';
                tablehtml += '</tr>';
            }
        }
        $('#sysList').html(tablehtml);
        $('#sysList').find("tr").eq(0).trigger("click").find("span").css("color", "#427fe5").css("font-weight", "bold");
    }
}


//获取所属模块
function getParamList(yydm, obj) {
    $(obj).parent().find("span").removeAttr("style")
    $(obj).find("span").css("color", "#427fe5").css("font-weight", "bold");
    if (yydm != '') {
        $.ajax({
            type: "GET",
            contentType: 'application/json',
            dataType: "json",
            url: "param/queryModule/" + yydm,
            success: function (jsonData) {
                if (jsonData.code == 200) {
                    var list = jsonData.list;
                    var tbodyhtml = '';
                    if (list.length > 0) {
                        for (var i = 0; i < list.length; i++) {
                            var data = list[i];
                            if (data != null) {
                                tbodyhtml += '<tr onclick="queryParamsList(\'' + yydm + '\',\'' + data.ssmk + '\',this)">';
                                tbodyhtml += '<td align="center" style="padding-left: 0px;padding-right: 0px;">';
                                tbodyhtml += '<span>' + data.ssmk + '</span>';
                                tbodyhtml += '</td>';
                                tbodyhtml += '</tr>';
                            }
                        }
                    }
                    $('#sysTbody').html(tbodyhtml);
                    $('#sysTbody').find("tr").eq(0).trigger("click");
                }
            }
        });
    }
}


//查询参数列表并生成表单
function queryParamsList(yydm, ssmk, obj) {
    ssmkval = ssmk;
    yydmval = yydm;
    $(obj).parent().find("span").removeAttr("style");
    $(obj).find("span").css("color", "#427fe5").css("font-weight", "bold");

    $.ajax({
        type: "GET",
        contentType: 'application/x-www-form-urlencoded',
        dataType: "json",
        url: "param/queryParamList",
        data: {
            yydm: yydm,
            ssmk: ssmk,
            csmc: ""
        },
        success: function (jsonData) {
            if (jsonData.code == 200) {
                var list = jsonData.list;
                divHtml(list);
            }
        }
    })
}

//保存参数设置
function saveParam() {
    let data = $("#paramForm").serializeJson();
    let param_jcpt_loginMode_csz = localStorage.getItem("param_jcpt_loginMode_csz");
    if (param_jcpt_loginMode_csz == null || param_jcpt_loginMode_csz == undefined) {
        param_jcpt_loginMode_csz = loginModeCsz;
    }
    data["jcpt_loginmode#3"] = (param_jcpt_loginMode_csz);
    let formJSON = JSON.stringify(data);
    if (formJSON != "") {
        $.ajax({
            type: "POST",
            contentType: 'application/json;charset=UTF-8',
            async: false,
            dataType: "json",
            url: "param/save",
            data: formJSON,
            success: function (jsonData) {
                if (jsonData.code == 200) {
                    LayerWn.showSucAlert("保存成功");
                } else {
                    LayerWn.showFailAlert("保存失败", jsonData.msg)
                }
            }
        });
    }

}


//  参数名称 搜索
function serach() {
    var csmc = $("#csmc").val();
    $.ajax({
        type: "GET",
        contentType: 'application/x-www-form-urlencoded',
        dataType: "json",
        url: "param/queryParamList",
        data: {
            yydm: yydmval,
            ssmk: ssmkval,
            csmc: csmc
        },
        success: function (jsonData) {
            if (jsonData.code == 200) {
                var list = jsonData.list;
                divHtml(list);
            }
        }
    })


}


//页面动态表单
function divHtml(list) {

    var tbodyhtml = '';
    if (list.length > 0) {
        for (var i = 0; i < list.length; i++) {
            var data = list[i];
            //文本框
            if ("0" == data.srfsdm) {
                //数据库密码配置参数，需要增加一键加密按钮
                if("dbPassWord"==data.csbm){
                        tbodyhtml += '<div class="row">';
                        tbodyhtml += '<label class="col-sm-7 pl60 mt10 mb10" style="font-weight: var(--fontWeight)  style="210px"" data-toggle="tooltip"  title="' + data.cssm + '">' + data.csmc + "(" + data.csbm + ')</label>';
                        tbodyhtml += '<div class="col-sm-5 pr60 " style="display:inline-block !important;">';
                        tbodyhtml += '<input type="text" class="form-control" style="width: 70%!important;display:inline-block !important;"" id="' + data.csbm +'" name="' + data.csbm + '#' + data.srfsdm + '" onclick="updateEncryptZt()" value="' + data.csz + '"/>';
                        if("miWen"==data.cszsm){
                            tbodyhtml += ' <button id="encryptID" class="btn" style="vertical-align: top" type="button" disabled="disabled" onclick="encrypt()">加密</button>';
                        }else{
                            tbodyhtml += ' <button id="encryptID" class="btn borderRadius"  style="vertical-align: top" type="button" onclick="encrypt()">加密</button>';
                        }
                        tbodyhtml += '</div>';
                        tbodyhtml += '</div>';
                }else{
                    tbodyhtml += '<div class="row">';
                    tbodyhtml += '<label class="col-sm-7 pl60 mt10 mb10" style="font-weight: var(--fontWeight)  style="210px"" data-toggle="tooltip"  title="' + data.cssm + '">' + data.csmc + "(" + data.csbm + ')</label>';
                    tbodyhtml += '<div class="col-sm-5 pr60 " style="text-align: right style="200px"">';
                    tbodyhtml += '<input type="text" class="form-control" style="190px" id="' + data.csbm + '#' + data.srfsdm + '" name="' + data.csbm + '#' + data.srfsdm + '" value="' + data.csz + '"/>';
                    tbodyhtml += '</div>';
                    tbodyhtml += '</div>';
                }

            }
            //下拉框
            if ("1" == data.srfsdm) {
                var optionData = data.optionList;
                if (null != optionData) {
                    if (optionData.length > 0) {
                        var optionHtml = '';
                        tbodyhtml += '<div class="row">';
                        tbodyhtml += '<label class="col-sm-7 pl60 mt10 mb10"  style="font-weight: var(--fontWeight)" data-toggle="tooltip"  title="' + data.cssm + '">' + data.csmc + "(" + data.csbm + ')</label>';
                        tbodyhtml += '<div class="col-sm-5 pr60" style="text-align: right">';
                        tbodyhtml += '<select id="' + data.csbm + '#' + data.srfsdm + '" name="' + data.csbm + '#' + data.srfsdm + '" class="form-control">';
                        $.each(optionData, function (index, option) {
                            if (data.csz == option.xmdm) {
                                optionHtml += '<option  value= "' + option.xmdm + '" selected = "selected">' + option.xmmc + '</option>';
                            } else {
                                optionHtml += '<option  value= "' + option.xmdm + '">' + option.xmmc + '</option>';
                            }
                        });
                        tbodyhtml += optionHtml;
                        tbodyhtml += '</select>';
                        tbodyhtml += '</div>';
                        tbodyhtml += '</div>';
                    }
                }
            }
            //选择框
            if ("2" == data.srfsdm) {
                tbodyhtml += '<div class="row">';
                tbodyhtml += '<label class="col-sm-8 pl60 mt10 mb10" style="font-weight: var(--fontWeight)" data-toggle="tooltip"  title="' + data.cssm + '">' + data.csmc + "(" + data.csbm + ')</label>';
                tbodyhtml += '<label class="col-sm-4 pr60 i-checks" style="text-align: right">';
                if ('1' == data.checked) {

                    tbodyhtml += '<input type="checkbox" checked="true"  id="' + data.csbm + '#' + data.srfsdm + '" name="' + data.csbm + '#' + data.srfsdm + '"/>' + '</label>';
                } else {
                    tbodyhtml += '<input type="checkbox"  id="' + data.csbm + '#' + data.srfsdm + '" name="' + data.csbm + '#' + data.srfsdm + '"/>' + '</label>';
                }
                tbodyhtml += '</div>';
            }
            //多选框
            if ("3" == data.srfsdm) {
                localStorage.setItem("param_jcpt_loginMode", JSON.stringify(data.optionList));
                loginModeCsz = data.csz;
                tbodyhtml += '<div class="row">';
                tbodyhtml += '<label class="col-sm-7 pl60 mt10 mb10" style="font-weight: var(--fontWeight)  style="210px"" data-toggle="tooltip"  title="' + data.cssm + '">' + data.csmc + "(" + data.csbm + ')</label>';
                tbodyhtml += '<div class="col-sm-5 pr60 " style="text-align: right ">';
                tbodyhtml += '<button class="btn btn-primary-wn borderRadius" type="button" onclick="extensionFieldButton()">修改</button>';
                tbodyhtml += '</div>';
                tbodyhtml += '</div>';
            }
        }
    }
    $('#paramDiv').html(tbodyhtml);
    $(function () {
        $("[data-toggle='tooltip']").tooltip();
    });
    showCss();
}

//加密
function encrypt() {
    var dbPassWord = $("#dbPassWord").val();
    if(dbPassWord == "" || dbPassWord == null){
        LayerWn.showAlert("请输入参数");
        return;
    }
    var update="encrypt";
    var data = {dbPassWord:dbPassWord,update:update};
    var formJSON = JSON.stringify(data);
    updateDbPassWord(formJSON);
}

//解密
function decrypt() {
    var dbPassWord = $("#dbPassWord").val();
    if(dbPassWord == "" || dbPassWord == null){
        LayerWn.showAlert("请输入参数");
        return;
    }
    var update="decrypt";
    var data = {dbPassWord:dbPassWord,update:update};
    var formJSON = JSON.stringify(data);
    updateDbPassWord(formJSON);
}

//更新加密状态
function updateEncryptZt() {
    $("#encryptID").removeAttr('disabled');
    cszsmval="";
}

//更新数据库密码
function updateDbPassWord(formJSON){
    $.ajax({
        type: "POST",
        contentType: 'application/json;charset=UTF-8',
        async: false,
        dataType: "json",
        url: "param/updateDbPassWord",
        data: formJSON,
        success: function (jsonData) {
            if (jsonData.code == 200) {
                queryParamsList(yydmval, ssmkval, this)
            } else {
                LayerWn.showFailAlert("操作失败", jsonData.msg)
            }
        }
    });
}
function extensionFieldButton() {
    LayerWn.openDialog('扩展字段', baseURL + 'plat-param-set-jcpt-loginMode.html?loginModeCsz=' + loginModeCsz, 500, 450);
}